#include "yamlstream.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>

void        YamlStream::newBlock(const QString& name)
{
    appendIndentation();
    _stream << name.toStdString() << ":" << std::endl;
    ++_indentLevel;
}

void        YamlStream::endBlock(void)
{
    _stream << std::endl;
    if (_indentLevel)
      --_indentLevel;
}

void        YamlStream::appendIndentation(void)
{
    unsigned short indent;

    for (indent = _indentLevel ; indent ; --indent)
        _stream << "  ";
}

char        YamlStream::SaveToFile(const QString &filepath)
{
    QFile file(filepath);

    if (!(file.open(QFile::WriteOnly)))
    {
        qDebug() << "YamlStream: Can't open " << filepath;
        return (1);
    }
    QTextStream out(&file);

    out << str().c_str();
    file.close();
    return (0);
}
